'use strict';

require('../../assets/scss/entry/common.scss');
require('../../assets/scss/entry/login.scss');

let $ = jQuery = window.jQuery = window.$ = require('jquery');

import * as api from '../../components/api/index.js';
import url from '../../components/util/url.js';

let pageEntry = (function() {
	// let mediaId = url.getQuery('mediaId');
	/**
	 * 验证机构名称
	 * @param exam {string} 机构名称
	 */
	let verifyExam = (exam) => {
		//验证机构名称是否为空
		if (!exam) {
			// Layer.msg('考生姓名不能为空');
			$(".error").hide();
			$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
			$(".examName").empty().html("*机构名称不能为空");
			$("#examName").css("borderColor","rgb( 255, 141, 50 )");
			$(".examName").show();
			return;
		}
		return true;
	}
	/**
	 * 验证手机号
	 * @param tel {Number} 手机号码
	 */
	let verifyTel = (tel) => {
		let telReg = /^(13[0-9]|15[0-9]|18[0-9]|14[0-9]|17[0-9])\d{8}$/;

		// 验证手机号是否为空
		if (!tel) {
			$(".error").hide();
			$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
			$(".parentTel").empty().html("*手机号不能为空");
			$("#parentTel").css("borderColor","rgb( 255, 141, 50 )");
			$(".parentTel").show();
			return;
		}
		// 验证手机号是否正确
		if (!telReg.test(tel)) {
			$(".error").hide();
			$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
			$(".parentTel").empty().html("*请输入正确的手机号");
			$("#parentTel").css("borderColor","rgb( 255, 141, 50 )");
			$(".parentTel").show();
			return;
		}

		return true;
	}

	/**
	 * 验证手机验证码
	 * @param checkCode {Number} 手机验证码
	 */
	let verifyCheckCode = (checkCode) => {
		let checkCodeReg = /^\d{4}$/;

		// 验证手机验证码是否为空
		if (!checkCode) {
			$(".error").hide();
			$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
			$(".checkCode").empty().html("*验证码不能为空");
			$("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
			$(".checkCode").show();
			return;
		}
		// 验证手机验证码是否正确
		if (!checkCodeReg.test(checkCode)) {
			$(".error").hide();
			$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
			$(".checkCode").empty().html("*请输入正确的验证码");
			$("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
			$(".checkCode").show();
			return;
		}

		return true;
	}

	/**
	 * 验证码倒计时
	 * @param time {Number} 倒计时时间
	 * @param checkCodeBtn {jquery Dom} 验证码按钮
	 * @param checkCodeBtnPlaceholder {jquery Dom} 倒计时按钮
	 */
	let countdown = (time, checkCodeBtn, checkCodeBtnPlaceholder) => {
	    var resend = time;
	    checkCodeBtn.hide();
	    // checkCodeBtnPlaceholder.show();
	    checkCodeBtnPlaceholder.css("display","inline-block");

	    var timer = setInterval(function() {
	        var second =  --resend;
	        checkCodeBtnPlaceholder.html(second + 's');

	        if(resend == 0) {
	            checkCodeBtnPlaceholder.html('60s').hide();
	            checkCodeBtn.show();
	            clearTimeout(timer);
	        }
	    }, 1000);
	}
	return {
		setWindowHeight() {
			// 获取视窗高度，将背景图铺满屏幕
			let wh = (window.innerHeight > 0) ? window.innerHeight : screen.height;
			$('body').height(wh);
		},
		bindEvent() {
			// 绑定注册事件
			$(".register-footer-content").off("click").on("click", function(event) {
				let examName = $("#examName").val();//机构名称
				let parentTel = $("#parentTel").val();//绑定手机号
				let checkCode = $("#checkCode").val();//手机验证码
				if(verifyExam(examName) && verifyTel(parentTel) && verifyCheckCode(checkCode)){
					api.register(examName,checkCode,parentTel,"1").then(resp=>{
						if(resp.ret == "0"){
							window.location = "/registerSuccess.html?registerType=1&agencyNumber="+resp.content.register.orgCode;
						}else{
							if(resp.code == "E102007"){//绑定手机已存在
								$(".error").hide();
								$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
								$(".checkCode").empty().html("*"+resp.msg);
								// $("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
								$(".checkCode").show();
							}else if(resp.code == 500){//验证码错误
								$(".error").hide();
								$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
								$(".checkCode").empty().html("*"+resp.msg);
								// $("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
								$(".checkCode").show();
							}else{//系统异常，请稍后再试
								$(".error").hide();
								$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
								$(".checkCode").empty().html("*"+resp.msg);
								// $("#checkCode").css("borderColor","rgb( 255, 141, 50 )");
								$(".checkCode").show();
							}
						}
						
					});
					// let url = "";
					// $.ajax({
					// 	type: "post",
					// 	url: 'http://api.teacher114.cn/api/kcw/v1/user/register',
					// 	data: {orgName: examName, idenCode: checkCode, telephone: parentTel, registerType: 1},
					// 	timeout: 15000,
					// 	dataType: "json",
					// })
					// .done(function(res) {
					// 	console.log(res);
						// var html = "";
						// if($.isEmptyObject(res.retData) || res.retData.listRankTable.length == 0) {
				  //           $("#"+ elemId +" table").hide();
				  //           $("#"+ elemId +" .nothing").show();
						// }else{
						// 	for(var i = 0; i < res.retData.listRankTable.length; i++) {
						// 		var rankT = res.retData.listRankTable[i];
						// 		html += "<tr>"+
						// 			'<td><span class="num">'+(i+1)+'</span></td>'+
						// 			'<td class="team-i"><img src="'+ rankT.VC2TEAMLOGOURL +'" alt=""><span>'+ rankT.VC2TEAMCHNAME + '</span></td>'+
						// 			'<td>'+ rankT.NUMMATCHFINISH +'</td>'+
						// 			'<td>'+ rankT.NUMSCORE +'</td>'+
						// 			'<td>'+ rankT.NUMWIN +'/'+ rankT.NUMDRAW +'/'+ rankT.NUMFAIL +'</td>'+
						// 			'<td>'+ rankT.NUMGOAL +'/'+ rankT.NUMLOST +'</td>'+
						// 		'</tr>';
						// 	}
						// }
						// $("#"+ elemId +" tbody").html(html);
					// })
					// .fail(function(res) {
					// 	throw new Error(res);
					// })
				}
			});
			//绑定发送验证码按钮
			$(".content-right .send").off("click").on("click",function(event){
				let examName = $("#examName").val();//机构名称
				let parentTel = $("#parentTel").val();//绑定手机号
				if(verifyExam(examName) && verifyTel(parentTel)){
					$(".error").hide();
					$(".content-right input").css("borderColor","rgb( 231, 231, 231 )");
					countdown(60,$(".send"),$(".time"));
					api.sendSmsCode(examName,parentTel).then(resp=>{
						// if(resp.ret == "0"){//发送成功
							// countdown(60,$(".send"),$(".time"));
						// }
					});
				}
			});
		}
	}
})();

pageEntry.setWindowHeight();
pageEntry.bindEvent();
